//
//  SXCircleTempView.m
//  SXGifImageShowDemo
//
//  Created by Ganide on 2019/4/12.
//  Copyright © 2019年 Ganide. All rights reserved.
//

#import "SXCircleTempView.h"
#import "UILabel+SXExtention.h"
#import "Masonry.h"
// 初始低 角度
#define STARTANYGLE  ((float)M_PI)/7
@interface SXCircleTempView()

// 大圆半径
@property(assign,nonatomic) CGFloat radious;
// 偏离 的基本单位
@property(assign,nonatomic) CGFloat cellAnygle;
// 记住 滑动以后 偏离多少度
@property(assign,nonatomic) CGFloat totalCellTotalAngle;
@property(assign,nonatomic) CGPoint selfCenter;
@property(assign,nonatomic) CGFloat lineWidth;
// 小球view 的frame值
@property(assign,nonatomic) CGRect valveRect;
// 是否触摸在小球上
@property(assign,nonatomic) BOOL spot;
//圆弧View
@property(weak,nonatomic) UIView *circleView;
// 减去温度Btn
@property(weak,nonatomic) UIButton *mulBtn;
// 添加Btn
@property(weak,nonatomic) UIButton *addButton;
// 显示多少度温度
@property(weak,nonatomic) UILabel *setTempValueLabel;
// 显示当前温度
@property(weak,nonatomic) UILabel *currentTemp;
@end
@implementation SXCircleTempView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
       // _cellAnygle = (2 * ((float)M_PI)- STARTANYGLE * 5)/100;
        _totalValue = 100;
        _circlyValue = 20;
        _cellValue = 0.5;
        self.lineWidth = 3;
        [self addUIView];
    }
    return self;
}
- (void)setTotalValue:(CGFloat)totalValue{
    _totalValue = totalValue;
    _cellAnygle = (2 * ((float)M_PI)- STARTANYGLE * 5)/totalValue;
}
- (void)drawRect:(CGRect)rect {
    
    [self drawSetUPUI];
    
}
- (void)setCirclyValue:(NSInteger)circlyValue{
    _circlyValue  = circlyValue;
    if (circlyValue >= _totalValue) {
        _circlyValue = _totalValue;
    }
    if (self.circelTempSetTempBlock) {
        self.circelTempSetTempBlock(circlyValue);
    }
    self.setTempValueLabel.text = [NSString stringWithFormat:@"%.1f",circlyValue*(_cellValue?_cellValue:0.5)];
    [self setNeedsDisplay];
}
- (void)setCellValue:(CGFloat)cellValue{
    _cellValue = cellValue;
    self.setTempValueLabel.text = [NSString stringWithFormat:@"%.1f",_circlyValue*(_cellValue?_cellValue:0.5)];
    [self setNeedsDisplay];
}
- (void)drawSetUPUI{
    // 画大圆
  //  CGFloat lineWidth =  3;
    
    CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    CGFloat radius = (self.bounds.size.width - _lineWidth)/2 - 5;
    self.selfCenter = center;
    _radious = radius;
    CGFloat startAngle = STARTANYGLE; //
    CGFloat endAngle = ((float)M_PI) - startAngle;
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    layer.name = @"Radius";
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineWidth = _lineWidth;
    path.lineCapStyle = kCGLineCapButt;
   
    [[UIColor lightGrayColor] set];
    
    [path addArcWithCenter:center radius:radius startAngle:endAngle endAngle:startAngle clockwise:YES];
    
    [path stroke];
    [path closePath];
    
    
    CGFloat grayStartAngle =M_PI - STARTANYGLE; //
    CGFloat grayEndAngle = _cellAnygle * _circlyValue + grayStartAngle;//((float)M_PI) - grayStartAngle;
    CAShapeLayer *grayLayer = [[CAShapeLayer alloc] init];
    grayLayer.name = @"Radius";
    
    UIBezierPath *grayPath = [UIBezierPath bezierPath];
    grayPath.lineWidth = _lineWidth;
    grayPath.lineCapStyle = kCGLineCapButt;
    
    [[UIColor whiteColor] set];
    
    [grayPath addArcWithCenter:center radius:radius startAngle:grayStartAngle endAngle:grayEndAngle clockwise:YES];
    
    [grayPath stroke];
    [grayPath closePath];
    
    CGFloat circelViewWidth = _lineWidth * 4;
    CGFloat pi_mYAngle = _cellAnygle * _circlyValue - startAngle;
    CGFloat circleViewX = 0;
    CGFloat circleViewY = 0;
    circleViewX = (_radious - _radious * cos(pi_mYAngle)) + _lineWidth - _lineWidth/2.0;
    circleViewY = _radious - _radious * sin(pi_mYAngle) + _lineWidth - _lineWidth/2.0;
    
    self.valveRect = CGRectMake(circleViewX, circleViewY, circelViewWidth, circelViewWidth);
    self.circleView.frame = self.valveRect;

}
- (void)addUIView{
    CGFloat maging = 5;
    CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    CGFloat radius = (self.bounds.size.width - _lineWidth)/2 - maging;
    self.selfCenter = center;
    _radious = radius;
    CGFloat startAngle = STARTANYGLE; //
  //  CGFloat endAngle = ((float)M_PI) - startAngle;
    CGFloat pi_mYAngle = _cellAnygle * _circlyValue - startAngle;
    CGFloat circleViewX = 0;
    CGFloat circleViewY = 0;
    circleViewX = (_radious - _radious * cos(pi_mYAngle)) + _lineWidth - _lineWidth/2.0;
    circleViewY = _radious - _radious * sin(pi_mYAngle) + _lineWidth - _lineWidth/2.0;
    
    CGFloat circelViewWidth = _lineWidth * 4;
    circleViewX = (_radious - _radious * cos(pi_mYAngle)) + _lineWidth - _lineWidth/2.0;
    circleViewY = _radious - _radious * sin(pi_mYAngle) + _lineWidth - _lineWidth/2.0;
    
    self.valveRect = CGRectMake(circleViewX, circleViewY, circelViewWidth, circelViewWidth);
    UIView *circleView = [[UIView alloc] initWithFrame:self.valveRect];
    self.circleView = circleView;
    circleView.layer.cornerRadius = circelViewWidth/2.0;
    circleView.layer.masksToBounds = YES;
    circleView.userInteractionEnabled = NO;
    [self addSubview:circleView];
    circleView.backgroundColor = [UIColor whiteColor];
    
    CGFloat btnWidth = 30;
    CGFloat mulLeftY = radius + radius * sin(STARTANYGLE) + 2;
    CGFloat mulLeftX = maging * 2;
    UIButton *mulBtn = [[UIButton alloc] initWithFrame:CGRectMake(mulLeftX, mulLeftY + _lineWidth, btnWidth, btnWidth)];
    [mulBtn addTarget:self action:@selector(addOrmulBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self btnSetComm:mulBtn content:@"-"];
    self.mulBtn = mulBtn;
   
    [self addSubview:mulBtn];
    
    //    UIView *autView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    // autView.backgroundColor = [UIColor redColor];
    UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.size.width - mulLeftX - btnWidth, mulLeftY + _lineWidth, btnWidth, btnWidth)];
    [addBtn addTarget:self action:@selector(addOrmulBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.addButton = addBtn;
    [self btnSetComm:addBtn content:@"+"];
    
    [self addSubview:addBtn];
    
    UILabel *setTempValueLabel = [UILabel label:@"29.5" font:40 textColor:[UIColor whiteColor]];
    setTempValueLabel.textAlignment = NSTextAlignmentCenter;
    self.setTempValueLabel = setTempValueLabel;
  //  setTempValueLabel.center = self.selfCenter;
  //  [setTempValueLabel sizeToFit];
    [self addSubview:setTempValueLabel];
    
    [setTempValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY).offset(-10);
    }];
    
    UILabel *tempSimbleLabel = [UILabel label:@"°C" font:13 textColor:[UIColor whiteColor]];
    [self addSubview:tempSimbleLabel];
    [tempSimbleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(setTempValueLabel.mas_top).offset(10);
        make.left.equalTo(setTempValueLabel.mas_right);
    }];
    UILabel *tipSetTempLabel = [UILabel label:@"设置温度" font:14 textColor:[UIColor whiteColor]];
    tipSetTempLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:tipSetTempLabel];
    [tipSetTempLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(setTempValueLabel.mas_top);
        make.centerX.equalTo(setTempValueLabel.mas_centerX);
    }];
    
    UILabel *currentLabel = [UILabel label:@"当前温度：25.3°C" font:12 textColor:[UIColor whiteColor]];
    self.currentTemp = currentLabel;
    [self addSubview:currentLabel];
    [currentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(setTempValueLabel.mas_bottom);
        make.centerX.equalTo(setTempValueLabel.mas_centerX);
    }];
    
}
- (void)addOrmulBtnClick:(UIButton *)sender{
    if (sender == self.addButton) {
        if (self.circlyValue >= _totalValue) {
            self.circlyValue = _totalValue;
        }else{
            self.circlyValue = self.circlyValue + 1;
        }
    }else{
        if (self.circlyValue <= 0) {
            self.circlyValue = 0;
        }else{
            self.circlyValue = self.circlyValue - 1;
        }
    }
}
- (void)btnSetComm:(UIButton *)sender content:(NSString *)content{
    [sender setTitle:content forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor whiteColor];
    sender.layer.cornerRadius = sender.frame.size.width/2.0;
    sender.clipsToBounds = YES;
    sender.titleLabel.font = [UIFont systemFontOfSize:30];
    [sender setTitleColor:[UIColor colorWithRed:59/255.0 green:139/255.0 blue:231/255.0 alpha:1] forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor whiteColor];
}
// 开始触摸
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    
    CGPoint point = [touch locationInView:self];
    
    point = CGPointMake(point.x, point.y);
    CGFloat temOffset = 15;
    CGRect toucheTect = CGRectMake(self.valveRect.origin.x - temOffset, self.valveRect.origin.y - temOffset, self.valveRect.size.width + temOffset * 2.0, self.valveRect.size.height + temOffset * 2.0);
    BOOL container =  CGRectContainsPoint(toucheTect, point);
    
    if (container) {
        
        self.spot = YES;
       // NSLog(@"dlfdlasfdljfaldsjfalfjdlsajfljfaljf");
    }
    else
    {
        self.spot = NO;
    }
}
// 开始滑动
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = [touches anyObject];
    
    CGPoint point = [touch locationInView:self];
    
    point = CGPointMake(point.x, point.y + 0);
    
    CGFloat radious = _radious;
    CGFloat XX = (_selfCenter.x - point.x) * (_selfCenter.x - point.x);
    CGFloat YY = (_selfCenter.y - point.y) * (_selfCenter.y - point.y);
    CGFloat OutLim = (radious ) * (radious);
    CGFloat inLim = (radious - 20) * (radious - 20);
    
    if (XX + YY < OutLim && XX + YY > inLim && (point.y < self.radious + self.radious * sin(STARTANYGLE) )) {
      
        if (self.spot) {
            //(2 * ((float)M_PI)- STARTANYGLE * 5)
            BOOL isBig = point.x > _selfCenter.x?M_PI_2:0;
            CGFloat roundAngle;
            roundAngle = isBig ? (2 * ((float)M_PI)- STARTANYGLE * 5) -  asinf((_selfCenter.y - point.y)/_radious) -  STARTANYGLE: asinf((_selfCenter.y - point.y)/_radious) + STARTANYGLE;
            self.circlyValue = (NSInteger)(round(roundAngle/_cellAnygle));
        }
        
        return;
    }
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.spot = NO;
    
  //  NSLog(@"拖拽结束=========");
}

- (void)setCurrentTempString:(CGFloat)currentTemp{
    self.currentTemp.text = [NSString stringWithFormat:@"当前温度：%.1f°C",currentTemp];
}
@end
